package java_class.student2_class;

public class test3_4 {
    public static void main(String[] args) {
        student[] arr=new student[3];

        student stu1=new student("孙亚森",22,001);
        student stu2=new student("李晴",22,002);
        student stu3=new student("张三",22,003);

        arr[0]=stu1;
        arr[1]=stu2;
        arr[2]=stu3;

    //    按照id删除学生信息并打印
        int index=getIndex(arr,2);
        if(index>=0){
            arr[index]=null;
        }else{
            System.out.println("没有这个学号 删除失败");
        }
        printArr(arr);


    }
    public static void printArr(student[] arr){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                System.out.println(arr[i].getId()+" "+arr[i].getName()+" "+arr[i].getAge());

            }
        }
    }
    public static int getIndex(student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                if(arr[i].getId()==id){
                    return i;
                }
            }
        }
        return -1;
    }
}
